package com.bonc.datasource.aspect;

import com.bonc.datasource.annotation.DataSource;
import com.bonc.datasource.config.DynamicContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 多数据源，切面处理类
 */
@Component
@Slf4j
@Aspect
@Order(-1)
public class DataSourceAspect {
    protected Logger logger = LoggerFactory.getLogger(getClass());

//    @Pointcut("@annotation(com.bonc.datasource.annotation.DataSource) "
//            + "|| @within(com.bonc.datasource.annotation.DataSource)")
//    public void dataSourcePointCut() {
//
//    }
//
//    @Around("dataSourcePointCut()")
//    public Object around(ProceedingJoinPoint point) throws Throwable {
//        MethodSignature signature = (MethodSignature) point.getSignature();
//        Class targetClass = point.getTarget().getClass();
//        Method method = signature.getMethod();
//
//        DataSource targetDataSource = (DataSource) targetClass.getAnnotation(DataSource.class);
//        DataSource methodDataSource = method.getAnnotation(DataSource.class);
//        if (targetDataSource != null || methodDataSource != null) {
//            String value;
//            if (methodDataSource != null) {
//                value = methodDataSource.value();
//            } else {
//                value = targetDataSource.value();
//            }
//
//            DynamicContextHolder.push(value);
//            logger.debug("set datasource is {}", value);
//        }
//
//        try {
//            logger.debug("datasource is {};method is {}", DynamicContextHolder.peek(),point.getSignature().getName());
//            return point.proceed();
//        } finally {
//            DynamicContextHolder.poll();
//            logger.debug("clean datasource");
//        }
//    }
@Before("@annotation(com.bonc.datasource.annotation.DataSource) "
        + "|| @within(com.bonc.datasource.annotation.DataSource)")
public void before(JoinPoint joinPoint) {
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    Class targetClass = joinPoint.getTarget().getClass();
    Method method = signature.getMethod();
    String name = method.getName();
    if(name.equals("batchAdd")) {
        int a = 1;
    }
    DataSource targetDataSource = (DataSource) targetClass.getAnnotation(DataSource.class);
    DataSource methodDataSource = method.getAnnotation(DataSource.class);
    String value = "";
    if (targetDataSource != null || methodDataSource != null) {
        if (methodDataSource != null) {
            value = methodDataSource.value();
        } else {
            value = targetDataSource.value();
        }

        //DataSourceContextHolder.setDataSource(value);
        DynamicContextHolder.push(value);
        logger.debug("set datasource is {}", value);
    }
}

    /**
     * 服务类的方法结束后，会清除数据源，此时会变更为默认的数据源
     **/
    @After("@annotation(com.bonc.datasource.annotation.DataSource) "
            + "|| @within(com.bonc.datasource.annotation.DataSource)")
    public void after(JoinPoint point){
        // execution 中配置的是服务实现类 & MyDataSource的包路径
        DynamicContextHolder.poll();
    }
}